package projectServlet;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Testing.NewLeaveList;
import Testing.ToolChen;

import dao.DAOException;
import dao.LeaveDAO;
import dao.mysql.LeaveDAOFactoryImpl;
import dto.LeaveDTO;
import dto.LeaveSearchDTO;

/**
 * Servlet implementation class LeaveHistoryForM
 */
@WebServlet("/LeaveHistoryForM")
public class LeaveHistoryForM extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LeaveHistoryForM() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name;
		String id;

		name=request.getParameter("name");
        id=request.getParameter("id");
        
		//validation}   
        boolean flagName=false;
    	boolean flagId=false;
    	boolean flagYearFrom=false;
    	boolean flagMonthFrom=false;
     	boolean flagDayFrom=false;
     	boolean flagYearTo=false;
     	boolean flagMonthTo=false;
     	boolean flagDayTo=false;
    if(name.trim()!="")
    {
    	if(isString(name)){flagName=true;}
    }
    if(id.trim()!="")
    {
    	if(isInt(id)){flagId=true;}
    }
    if(request.getParameter("yearFrom").trim()!="")
    {	
    	if(isYear(request.getParameter("yearFrom"))) {flagYearFrom=true;}
    }
    if(request.getParameter("monthFrom").trim()!="")
    {
    	if(request.getParameter("yearFrom").trim()=="")
    	{flagMonthFrom=true;}
    	else if(request.getParameter("yearFrom").trim()!=""){
    	if(isMonth(request.getParameter("monthFrom"))) {flagMonthFrom=true;}
    	}
    }
    if(request.getParameter("dayFrom").trim()!="")
    {
    	if(request.getParameter("yearFrom").trim()=="")
    	{flagDayFrom=true;}
    	else if (request.getParameter("monthFrom").trim()=="")
    	{flagDayFrom=true;}
    	else if(request.getParameter("yearFrom").trim()!=""&&request.getParameter("monthFrom").trim()!=""){
    	//yearToGetDays=request.getParameter("yearFrom");
    	//monthToGetDays=request.getParameter("monthFrom");
    		getDay=getDays(request.getParameter("yearFrom"),request.getParameter("monthFrom"));
    	//getDays(yearToGetDays,monthToGetDays);
    	if(isDay(request.getParameter("dayFrom"))) {flagDayFrom=true;}
    	}
    }	
    if(request.getParameter("yearTo").trim()!="")
    { 	
    	if(isYear(request.getParameter("yearTo"))) {flagYearTo=true;}
    }
    if(request.getParameter("monthTo").trim()!="")
    {
    	if(request.getParameter("yearTo").trim()=="")
    	{flagMonthTo=true;}
    	else if(request.getParameter("yearTo").trim()!=""){
    	if(isMonth(request.getParameter("monthTo"))) {flagMonthTo=true;}
    	}
    }
    if(request.getParameter("dayTo").trim()!="")
    {
    	if(request.getParameter("yearTo").trim()=="")
    	{flagDayTo=true;}
    	else if (request.getParameter("monthTo").trim()=="")
    	{flagDayTo=true;}
    	else if(request.getParameter("yearTo").trim()!=""&&request.getParameter("monthTo").trim()!=""){
    		getDay=getDays(request.getParameter("yearTo"),request.getParameter("monthTo"));
    	//yearToGetDays=request.getParameter("yearTo");
    	//monthToGetDays=request.getParameter("monthTo");
    	//getDays(yearToGetDays,monthToGetDays);
    	if(isDay(request.getParameter("dayTo"))) {flagDayTo=true;}
    	}
    }	
		
    request.setAttribute("flagName", flagName);
    request.setAttribute("flagId", flagId);
    request.setAttribute("flagYearFrom", flagYearFrom);
    request.setAttribute("flagMonthFrom", flagMonthFrom);
    request.setAttribute("flagDayFrom", flagDayFrom);
    request.setAttribute("flagYearTo", flagYearTo);
    request.setAttribute("flagMonthTo", flagMonthTo);
    request.setAttribute("flagDayTo", flagDayTo);   
    
    
    Date dateFrom = null;
    Date dateTo = null;
    int idd= 0 ;
    String nammme = null;
    
		if(!(flagName || flagId || flagYearFrom || flagMonthFrom || flagDayFrom || flagYearTo || flagMonthTo || flagDayTo)){
			Calendar calendar1 = Calendar.getInstance();
			Calendar calendar2 = Calendar.getInstance();
			if(request.getParameter("yearFrom").trim()!=""&&request.getParameter("monthFrom").trim()==""&&request.getParameter("dayFrom").trim()==""){
			calendar1.set(Integer.parseInt(request.getParameter("yearFrom")),
					0,
					1);
			dateFrom = calendar1.getTime();
			}
			if(request.getParameter("yearFrom").trim()!=""&&request.getParameter("monthFrom").trim()!=""&&request.getParameter("dayFrom").trim()==""){
				calendar1.set(Integer.parseInt(request.getParameter("yearFrom")),
						Integer.parseInt(request.getParameter("monthFrom")) - 1,
						1);
				dateFrom = calendar1.getTime();
				}
			if(request.getParameter("yearFrom").trim()!=""&&request.getParameter("monthFrom").trim()!=""&&request.getParameter("dayFrom").trim()!=""){
				calendar1.set(Integer.parseInt(request.getParameter("yearFrom")),
						Integer.parseInt(request.getParameter("monthFrom")) - 1,
						Integer.parseInt(request.getParameter("dayFrom")));
				dateFrom = calendar1.getTime();
				}
			
			
			if(request.getParameter("yearTo").trim()!=""&&request.getParameter("monthTo").trim()==""&&request.getParameter("dayTo").trim()==""){
				calendar1.set(Integer.parseInt(request.getParameter("yearTo")),
						0,
						1);
				dateTo = calendar1.getTime();
				}
				if(request.getParameter("yearTo").trim()!=""&&request.getParameter("monthTo").trim()!=""&&request.getParameter("dayTo").trim()==""){
					calendar1.set(Integer.parseInt(request.getParameter("yearTo")),
							Integer.parseInt(request.getParameter("monthTo")) - 1,
							1);
					dateTo = calendar1.getTime();
					}
				if(request.getParameter("yearTo").trim()!=""&&request.getParameter("monthTo").trim()!=""&&request.getParameter("dayTo").trim()!=""){
					calendar1.set(Integer.parseInt(request.getParameter("yearTo")),
							Integer.parseInt(request.getParameter("monthTo")) - 1,
							Integer.parseInt(request.getParameter("dayTo")));
					dateTo = calendar1.getTime();
					}
				
				if(request.getParameter("id").trim()!="")
				{
					idd=Integer.parseInt(request.getParameter("id"));
				}
				
				if(request.getParameter("name").trim()!="")
				{
					nammme = request.getParameter("name");
				}
			
			
	    //boolean flagList=true;
	    request.setAttribute("flagList", true);
		LeaveSearchDTO ldto = new LeaveSearchDTO();
		ldto.set_userName(nammme);
		ldto.set_userID(idd);
		ldto.set_startDate(dateFrom);
		ldto.set_endDate(dateTo);
		
		//Simon modified
		String sManagerID =  (String) request.getSession().getAttribute("id");
		int ManagerID = Integer.parseInt(sManagerID);
		ldto.set_ApprovingmanID(ManagerID);
		
		
		LeaveDAOFactoryImpl fac = new LeaveDAOFactoryImpl();
		LeaveDAO dao = fac.getLeaveDAO();
		ArrayList<LeaveDTO> dtoList = null;
		try {
			dtoList = dao.findLeaveByCriteria(ldto);

		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (dtoList != null) {
			NewLeaveList nlist = new NewLeaveList();
			ArrayList<LeaveDTO> listSort = nlist.getSortList(dtoList);
			request.setAttribute("LeaveInfo", listSort);
			
		}
		
		
		
	}
		RequestDispatcher rd=request.getRequestDispatcher("/LeaveHistoryForM.jsp");
		rd.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	public static boolean isString(String s)
	{
		
		for(int i=0;i<s.length();i++)
		{
			if(s.charAt(i) >= '0' && s.charAt(i) <= '9')
			{
				return true;
			}
		}
		return false;
	}
	
	public static boolean isInt(String s)
	{
		
		for(int i=0;i<s.length();i++)
		{
			if(!(s.charAt(i) >= '0' && s.charAt(i) <= '9'))
			{
				return true;
			}
			
		}
		return false;
	}
	
	public static boolean isYear(String s)
	{
		if(s.length()!=4)
		{
			return true;
		}
		
		for(int i=0;i<s.length();i++)
		{
			if(!(s.charAt(i) >= '0' && s.charAt(i) <= '9'))
			{
				return true;
				
			}
			//else{return false;}
		}
		return false;
	}
	
	public static boolean isMonth(String s)
	{
		if(!(s.length()==1||s.length()==2))
		{
			return true;
		}
		for(int i=0;i<s.length();i++)
			{
				if(!(s.charAt(i) >= '0' && s.charAt(i) <= '9'))
				{
					return true;
				}
		
		    }
		
			//else if(!(s.length()==1||s.length()==2))
			//return true;
		return false;
			
	}

	
	// static String yearToGetDays;
	 //static String monthToGetDays;
	static int getDay;
	 
	public static boolean isDay(String s)
	{
		
		if(!(s.length()==1||s.length()==2))
		{
			return true;
		}
		for(int i=0;i<s.length();i++)
		{
			if(!(s.charAt(i) >= '0' && s.charAt(i) <= '9'))
			{
				return true;
			}	
			//return false;	
		}

		if(!(Integer.parseInt(s) >= 1 && Integer.parseInt(s) <= getDay))
			{
				return true;
			}

		
		
		return false;
	}
	
	public static int getDays(String year, String month)
	{
		Calendar cal = Calendar.getInstance();  
		cal.set(Calendar.YEAR, Integer.parseInt(year));  
		cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);  
		cal.set(Calendar.DATE, 1);  
		cal.add(Calendar.MONTH, 1);  
		cal.add(Calendar.DATE, -1);  
		return cal.get(Calendar.DAY_OF_MONTH); 
	}
	
	

}
